package cn.zzdt4j.config.mapper;

import cn.zzdt4j.config.model.HisRunDataInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.Data;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * The interface His run data mapper.
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023 /11/6 20:23
 */
@Mapper
public interface HisRunDataMapper extends BaseMapper<HisRunDataInfo> {
    /**
     * Query thread pool max ranking list.
     *
     * @param startTime the start time
     * @param endTime   the end time
     * @return the list
     */
    @Select("SELECT " +
            "tenant_id, item_id, tp_id, max(queue_size) as max_queue_size, max(reject_count) as max_reject_count,  max(completed_task_count) as max_completed_task_count " +
            "FROM his_run_data " +
            "where timestamp between #{startTime} and #{endTime} " +
            "group by tenant_id, item_id, tp_id " +
            "order by max_completed_task_count desc " +
            "limit 4")
    List<HisRunDataMapper.ThreadPoolTaskRanking> queryThreadPoolMaxRanking(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    /**
     * Thread Pool Task Ranking
     */
    @Data
    class ThreadPoolTaskRanking {

        /**
         * Tenant id
         */
        private String tenantId;

        /**
         * Item id
         */
        private String itemId;

        /**
         * Tp id
         */
        private String tpId;

        /**
         * Max completed task count
         */
        private Long maxCompletedTaskCount;

        /**
         * Max queue size
         */
        private Long maxQueueSize;

        /**
         * Max reject count
         */
        private Long maxRejectCount;
    }
}
